<xsl:stylesheet version="1.1"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/2000/svg"
                xmlns:xlink="http://www.w3.org/1999/xlink"
                xmlns:random="http://exslt.org/random"
                xmlns:math="http://exslt.org/math"
                xmlns:exsl="http://exslt.org/common"
                xmlns:temp="http://temp.org/temp"
                extension-element-prefixes="exsl"
                exclude-result-prefixes="random math exsl">

    <xsl:preserve-space elements="tokens"/>
    <xsl:output method="xml"/>

    <xsl:param name="image-width"/>
    <xsl:param name="image-height"/>
    <xsl:param name="scale"/>

    <xsl:param name="background"/>

    <xsl:param name="player-type"/>

    <xsl:param name="player-cross-width"/>
    <xsl:param name="player-cross-height"/>
    <xsl:param name="player-cross-color"/>
    <xsl:param name="player-cross-stroke-width"/>

    <xsl:param name="player-square-width"/>
    <xsl:param name="player-square-height"/>
    <xsl:param name="player-square-color"/>
    <xsl:param name="player-square-stroke-width"/>

    <xsl:param name="player-turret-outer-radius"/>
    <xsl:param name="player-turret-inner-radius"/>
    <xsl:param name="player-turret-length"/>
    <xsl:param name="player-turret-outline"/>
    <xsl:param name="player-turret-outline-width"/>
    <xsl:param name="player-turret-outer-color"/>
    <xsl:param name="player-turret-inner-color"/>

    <xsl:param name="motion-blindness-active"/>

    <xsl:param name="motion-blindness-dots"/>
    <xsl:param name="motion-blindness-dot-radius"/>
    <xsl:param name="motion-blindness-dot-offset"/>
    <xsl:param name="motion-blindness-dot-color"/>
    <xsl:param name="motion-blindness-dot-flicker"/>

    <xsl:param name="motion-blindness-grid-rows"/>
    <xsl:param name="motion-blindness-grid-columns"/>
    <xsl:param name="motion-blindness-grid-spacing"/>
    <xsl:param name="motion-blindness-grid-color"/>
    <xsl:param name="motion-blindness-line-width"/>

    <xsl:template match="/">

        <svg width="{$image-width}" height="{$image-height}"
                viewBox="0 0 {$image-width} {$image-height}"
                version="1.1">
            <g>
                <rect x="0" y="0" width="{$image-width}" height="{$image-height}" fill="{$background}" stroke="none"/>
                <xsl:message><xsl:value-of select="$motion-blindness-active"/></xsl:message>
                <xsl:if test="$motion-blindness-active = 'true'">
                    <xsl:call-template name="motion-blindness"/>
                </xsl:if>
                <xsl:call-template name="player"/>
            </g>
        </svg>

    </xsl:template>

    <xsl:template name="player">
        <xsl:param name="cx" select="$image-width div 2"/>
        <xsl:param name="cy" select="$image-height div 2"/>
        <g>
            <xsl:choose>
                <xsl:when test="$player-type = 'cross'">
                    <g>

                        <line x1="{$cx - $player-cross-width * $scale div 2}"
                              y1="{$cy - $player-cross-height * $scale div 2}"
                              x2="{$cx + $player-cross-width * $scale div 2}"
                              y2="{$cy + $player-cross-height * $scale div 2}"
                              stroke="{$player-cross-color}"
                              stroke-width="{$player-cross-stroke-width * $scale}"/>
                        <line x1="{$cx - $player-cross-width * $scale div 2}"
                              y1="{$cy + $player-cross-height * $scale div 2}"
                              x2="{$cx + $player-cross-width * $scale div 2}"
                              y2="{$cy - $player-cross-height * $scale div 2}"
                              stroke="{$player-cross-color}"
                              stroke-width="{$player-cross-stroke-width * $scale}"/>
                    </g>
                </xsl:when>
                <xsl:when test="$player-type = 'square'">
                    <g transform="translate({$cx},{$cy})">
                        <rect x="{-$player-square-width * $scale div 2}"
                              y="{-$player-square-height * $scale div 2}"
                              width="{$player-square-width * $scale}"
                              height="{$player-square-height * $scale}"
                              fill="none"
                              stroke="{$player-square-color}">
                            <animateTransform attributeName="transform" attributeType="XML"
                                              type="rotate" from="0" to="-360" dur="4s"
                                              additive="replace" fill="freeze" repeatCount="indefinite"/>
                        </rect>
                    </g>
                </xsl:when>
                <xsl:when test="$player-type = 'turret'">
                    <g transform="translate({$cx},{$cy})">
                        <g>
                            <animateTransform attributeName="transform" attributeType="XML"
                                              type="rotate" from="0" to="-360" dur="12s"
                                              additive="replace" fill="freeze" repeatCount="indefinite"/>
                            <line x1="0" y1="0" x2="{$player-turret-length * $scale}" y2="0" stroke="{$player-turret-outline}" stroke-width="{$player-turret-outline-width * 2 * $scale}"/>
                            <circle cx="0" cy="0" r="{$player-turret-outer-radius * $scale}" stroke="{$player-turret-outline}" stroke-width="{$player-turret-outline-width * $scale}" fill="{$player-turret-outer-color}"/>
                            <circle cx="0" cy="0" r="{$player-turret-inner-radius * $scale}" stroke="{$player-turret-outline}" stroke-width="{$player-turret-outline-width * $scale}" fill="{$player-turret-inner-color}"/>
                        </g>
                        <text x="0" y="0" fill="{$player-turret-outline}" text-anchor="middle" alignment-baseline="middle">10</text>
                    </g>
                </xsl:when>
            </xsl:choose>
        </g>
    </xsl:template>

    <xsl:template name="motion-blindness">
        <g transform="translate({$image-width div 2},{$image-height div 2})">
            <g>
                <animateTransform attributeName="transform" attributeType="XML"
                                  type="rotate" from="0" to="360" dur="8s"
                                  additive="replace" fill="freeze" repeatCount="indefinite"/>
                <xsl:variable name="x1" select="-($motion-blindness-grid-spacing * $motion-blindness-grid-rows) div 2"/>
                <xsl:variable name="y1" select="-($motion-blindness-grid-spacing * $motion-blindness-grid-columns) div 2"/>
                <xsl:call-template name="lines">
                    <xsl:with-param name="x1" select="$x1 * $scale"/>
                    <xsl:with-param name="y1" select="$y1 * $scale"/>
                    <xsl:with-param name="x2" select="($motion-blindness-grid-spacing * $motion-blindness-grid-rows * $scale) div 2"/>
                    <xsl:with-param name="y2" select="$y1 * $scale"/>
                    <xsl:with-param name="dx" select="0"/>
                    <xsl:with-param name="dy" select="$motion-blindness-grid-spacing * $scale"/>
                    <xsl:with-param name="count" select="$motion-blindness-grid-rows + 1"/>
                    <xsl:with-param name="line-color" select="$motion-blindness-grid-color"/>
                    <xsl:with-param name="line-width" select="$motion-blindness-line-width * $scale"/>
                </xsl:call-template>
                <xsl:call-template name="lines">
                    <xsl:with-param name="x1" select="$x1 * $scale"/>
                    <xsl:with-param name="y1" select="$y1 * $scale"/>
                    <xsl:with-param name="x2" select="$x1 * $scale"/>
                    <xsl:with-param name="y2" select="($motion-blindness-grid-spacing * $motion-blindness-grid-columns * $scale) div 2"/>
                    <xsl:with-param name="dx" select="$motion-blindness-grid-spacing * $scale"/>
                    <xsl:with-param name="dy" select="0"/>
                    <xsl:with-param name="count" select="$motion-blindness-grid-columns + 1"/>
                    <xsl:with-param name="line-color" select="$motion-blindness-grid-color"/>
                    <xsl:with-param name="line-width" select="$motion-blindness-line-width * $scale"/>
                </xsl:call-template>
            </g>
            <xsl:call-template name="motion-blindness-dots">
                <xsl:with-param name="count" select="$motion-blindness-dots"/>
            </xsl:call-template>
        </g>
    </xsl:template>

    <xsl:template name="motion-blindness-dots">
        <xsl:param name="count"/>
        <xsl:param name="dangle" select="360 div $motion-blindness-dots"/>
        <xsl:if test="$count > 0">
            <g transform="rotate({$count * $dangle})">
                <xsl:if test="$motion-blindness-dot-flicker = 'true'">
                    <!-- animate in/out -->
                    <xsl:variable name="fade-out-time" select="math:random() * 59"/>
                    <xsl:variable name="fade-in-time" select="$fade-out-time + math:random() * (60 - $fade-out-time)"/>

                    <animate attributeType="CSS" attributeName="fill-opacity"
                             from="1" to="0" dur="0.5s" fill="freeze" begin="{$fade-out-time}s"/>
                    <animate attributeType="CSS" attributeName="fill-opacity"
                             from="0" to="1" dur="0.5s" fill="freeze" begin="{$fade-in-time}s"/>

                </xsl:if>
                <circle
                        cx="0"
                        cy="{$motion-blindness-dot-offset * $scale}"
                        r="{$motion-blindness-dot-radius * $scale}"
                        fill="{$motion-blindness-dot-color}"
                        stroke="none"
                >
                    <!--
                    <animateTransform attributeName="transform" attributeType="XML"
                                      type="rotate" from="0" to="-360" dur="120s"
                                      additive="replace" fill="freeze" repeatCount="indefinite"/>
                                      -->
                </circle>
            </g>
            <xsl:call-template name="motion-blindness-dots">
                <xsl:with-param name="count" select="$count - 1"/>
                <xsl:with-param name="dangle" select="$dangle"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <xsl:template name="lines">
        <xsl:param name="x1"/>
        <xsl:param name="y1"/>
        <xsl:param name="x2"/>
        <xsl:param name="y2"/>
        <xsl:param name="dx"/>
        <xsl:param name="dy"/>
        <xsl:param name="count"/>
        <xsl:param name="line-color"/>
        <xsl:param name="line-width"/>

        <xsl:if test="$count > 0">
            <line x1="{$x1}" y1="{$y1}" x2="{$x2}" y2="{$y2}" stroke="{$line-color}" stroke-width="{$line-width}"/>
            <xsl:call-template name="lines">
                <xsl:with-param name="x1" select="$x1 + $dx"/>
                <xsl:with-param name="y1" select="$y1 + $dy"/>
                <xsl:with-param name="x2" select="$x2 + $dx"/>
                <xsl:with-param name="y2" select="$y2 + $dy"/>
                <xsl:with-param name="dx" select="$dx"/>
                <xsl:with-param name="dy" select="$dy"/>
                <xsl:with-param name="count" select="$count - 1"/>
                <xsl:with-param name="line-color" select="$line-color"/>
                <xsl:with-param name="line-width" select="$line-width"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>